﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using ComLib.ComType;
using LogLib;

namespace ProcessConfig.ConfigType
{
    /// <summary>
    /// TCP客户端连接选项
    /// </summary>
    public class TcpClientOption : IniConfigBase
    {
        public string ServerIP { get; set; } = "localhost";
        public int Port { get; set; } = 5678;

        public int Timeout { get; set; } = 5000;

        public TcpClientOption()
        {
            
        }

        public TcpClientOption(TcpClientOption clone)
        {
            ServerIP = clone.ServerIP;
            Port = clone.Port;
            Timeout = clone.Timeout;
        }

        public override bool Save()
        {
            base.Save();
            SaveField("ServerIP", ServerIP);
            SaveField("Port", Port);
            SaveField("Timeout", Timeout);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                ServerIP = LoadStringValue("ServerIP");
                Port = LoadInt32Value("Port");
                Timeout = LoadInt32Value("Timeout");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("TCP客户端连接选项::");         
            Action<string, object> FillFieldString =
             (string field, object value) =>
             {
                 builder.Append("[");
                 builder.Append(field);
                 builder.Append("]: ");
                 builder.Append(value.ToString());
                 builder.Append("; ");
             };
            FillFieldString("连接地址", ServerIP);
            FillFieldString("Port", Port);
            FillFieldString("Timeout", Timeout);
            return builder.ToString();
        }
    }
}
